import requests
from Tools.log import MyLog


class HTTPRequest:

    def send_email_code(self):
        res_url = 'https://openapiv5.ketangpai.com//UserApi/sendEmailCode'
        res_data = {"verify": "5", "sessionid": "49re0jvtu5nfrb7knf2dcvcrv4", "email": "mysouls@88.com", "type": "reg",
                    "reqtimestamp": 1723865841008}

        res_obj = requests.post(res_url, res_data)
        return res_obj

    def get_figure_code(self):
        res_url = 'https://openapiv5.ketangpai.com/UserApi/getFigureCode'
        res_data = {"reqtimestamp": 1723865841246}

        res_obj = requests.post(res_url, res_data)
        return res_obj

    def test_api(self, url, data, http_method, cookie=None):
        # 注册/登录/创建课程
        try:
            session_obj = requests.session()
            if http_method.upper() == 'GET':
                res_obj = requests.get(url, data, cookies=cookie)
                return res_obj
            elif http_method.upper() == 'POST':
                res_obj = requests.post(url, data, cookies=cookie)
                return res_obj
            else:
                MyLog.info(f"请求方式不正确，只能通过GET或POST请求")
        except Exception as e:
            # 引入日志收集类
            MyLog.error(f"请求报错了，错误信息：{e}")
            raise e


# if __name__ == '__main__':
#     s = HTTPRequest('https://openapiv5.ketangpai.com//UserApi/login',
#                     '{"email": "790874262@qq.com", "password": "beiaidigan123!", "remember": "0"}').test_requests(
#         'https://openapiv5.ketangpai.com//CourseApi/createCourse',
#         {"coursename": "柠檬版自动化", "classname": "2024年夏季班", "semester": "2023-2024",
#          "term": "0", "teachtype": "1", "studytime": "200", "venue": "柠檬之城",
#          "describes": "<p>主打一个自动化测试</p>"
#             , "canview": "1", "neednatureclass": "", "needgrade": ""
#             , "needentrance": "", "reqtimestamp": 1721658997468}, "POST")
#     print(s.json())
if __name__ == '__main__':
    test = HTTPRequest().test_api('https://openapiv5.ketangpai.com//UserApi/login',
                                  {"email": "790874262@qq.com", "password": "beiaidigan123!", "remember": "0"}, 'POST'
                                  )
    print(test.json())
